package com.cskaoyan.market.service;


import com.cskaoyan.market.db.domain.MarketFootprint;
import com.cskaoyan.market.db.domain.MarketFootprintExample;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.mapper.MarketFootprintMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.WxFootPrintListbriefVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WxFootPrintServiceImpl implements WxFootPrintService {
    @Autowired
    MarketFootprintMapper footprintMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    FlashMapFactory flashMapFactory;

    @Override
    public List<WxFootPrintListbriefVo> getFootprintsByUserId(Integer userId) {
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.createCriteria().andUserIdEqualTo(userId);
//        List<MarketFootprint> marketFootprints =null;
//        boolean footPrintListIsFlash = (boolean) flashMapFactory.getValue("footPrintListIsFlash");
//
//        if (!footPrintListIsFlash) {
//            marketFootprints  = footprintMapper.selectByExample(marketFootprintExample);
//            flashMapFactory.putValue("flashFootprintsList",marketFootprints);
//        }
//            marketFootprints = (List<MarketFootprint>) flashMapFactory.getValue("flashOrderList");
        List<MarketFootprint> marketFootprints =footprintMapper.selectByExample(marketFootprintExample);
        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(new MarketGoodsExample());
        Map<Integer, MarketGoods> goodsMap = idGetGoodsMap(marketGoods);
        List<MarketGoods> resultList = new ArrayList<>();
        for (MarketFootprint footprint : marketFootprints) {
            if (!footprint.getDeleted()) {
                MarketGoods marketGood = goodsMap.get(footprint.getGoodsId());
                resultList.add(marketGood);
            }
        }

        List<WxFootPrintListbriefVo> resultListVo = new ArrayList<>();
        for (MarketGoods goods : resultList) {
            Integer id = goods.getId();
            LocalDateTime addTime = null;
            for (MarketFootprint marketFootprint : marketFootprints) {
                if (id.equals(marketFootprint.getGoodsId())) {
                    addTime = marketFootprint.getUpdateTime();
                }
            }
            WxFootPrintListbriefVo resultVo = new WxFootPrintListbriefVo(addTime, goods.getBrief(), Integer.parseInt(goods.getGoodsSn()),
                    goods.getId(), goods.getName(), goods.getPicUrl(), goods.getRetailPrice());
            resultListVo.add(resultVo);
        }
        return resultListVo;
    }

    private Map<Integer, MarketGoods> idGetGoodsMap(List<MarketGoods> marketGoods) {
        Map<Integer, MarketGoods> map = new HashMap<>();
        for (MarketGoods marketGood : marketGoods) {
            Integer goodsId = marketGood.getId();
            map.put(goodsId, marketGood);
        }
        return map;
    }



    @Transactional

    @Override
    public Object delete(MarketFootprint marketFootprint) {
        MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
        marketFootprintExample.createCriteria().andGoodsIdEqualTo(marketFootprint.getId());
        MarketFootprint footprint = new MarketFootprint();
        footprint.setDeleted(true);
        footprintMapper.updateByExampleSelective(footprint,marketFootprintExample);
        return ResponseUtil.ok();
    }
}
